package com.r2coding.ent.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.r2coding.common.exception.BizCode;
import com.r2coding.common.exception.ServiceException;
import com.r2coding.ent.dao.AlbumDao;
import com.r2coding.ent.dao.PictureDao;
import com.r2coding.ent.entity.AlbumEntity;
import com.r2coding.ent.entity.PictureEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author:98k灬
 * @since:2022/07/01 20:18
 **/
@Component
public class CommonUtil {

    @Autowired
    AlbumDao albumDao;

    @Autowired
    PictureDao pictureDao;

    /**
     * 检查相册的容量是否溢出
     *
     * @param albumId 相册id
     */
    public synchronized void checkAlbumSize(Integer albumId) {
        AlbumEntity album = albumDao.selectById(albumId);
        QueryWrapper<PictureEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("album_id", albumId);
        List<PictureEntity> list = pictureDao.selectList(wrapper);
        if (!CollectionUtils.isEmpty(list)) {
            if (list.size() == album.getSize()) {
                throw new ServiceException(BizCode.ALBUM_SIZE_MAX);
            }
        }
    }
}
